#include "snake.h"

snake::snake(QWidget *parent) : QWidget(parent)
{
    snakexy snakenode;
    for(int i=5;i>=0;i--)
    {
        snakenode.x=25*8+i*25;
        snakenode.y=25*8;
        snakevec.push_back(snakenode);
    }
}

inline void snake::dirmove(char key, snakexy &nexthead)
{
    switch (key)
    {
    case 'w':
        nexthead.x=snakevec.front().x;
        nexthead.y=snakevec.front().y-25;
        break;
    case 's':
        nexthead.x=snakevec.front().x;
        nexthead.y=snakevec.front().y+25;
        break;
        break;
    case 'a':
        nexthead.x=snakevec.front().x-25;
        nexthead.y=snakevec.front().y;
        break;
        break;
    case 'd':
        nexthead.x=snakevec.front().x+25;
        nexthead.y=snakevec.front().y;
        break;
    default:
        break;
    }
}

void snake::move(char key)
{
    snakexy nexthead;
    dirmove(key,nexthead);
    snakevec.insert(snakevec.begin(),nexthead);
}
void snake::reset()
{
    snakevec.clear();
    snakexy snakenode;
    for(int i=5;i>=0;i--)
    {
        snakenode.x=25*8+i*25;
        snakenode.y=25*8;
        snakevec.push_back(snakenode);
    }
}
